#include "UartScreen.h"

#include "StringConverter.h"

const uint8_t uartscreenstart[5] = {0xEE, 0xB1, 0x10, 0x00, 0x00}; //start bytes
const uint8_t uartscreenend[4] = { 0xFF, 0xFC, 0xFF, 0xFF }; //end bytes
uint8_t uartscreenDatas[100]; //发送数据合计


uint16_t zeroIds[] = {UartScreen_Gear_ID,UartScreen_MtclMode_ID,UartScreen_Speed_ID,UartScreen_Torque_ID,UartScreen_MaxTorque_ID,UartScreen_ActPower_ID,
		UartScreen_DcVoltage_ID,UartScreen_DcCurrent_ID,UartScreen_AcVoltage_ID,UartScreen_AcCurrent_ID,UartScreen_WindingTemp_ID,UartScreen_MtclInvTemp_ID,
		UartScreen_ErrorLevel_ID,UartScreen_ErrorCode_ID,UartScreen_ErrorBits_ID,UartScreen_ErrorStr_ID,UartScreen_MtclVersion_ID};

uint16_t dataShow[] = {UartScreen_NormalData1_ID,UartScreen_NormalData2_ID,UartScreen_NormalData3_ID,UartScreen_NormalData4_ID,
		UartScreen_NormalData5_ID,UartScreen_NormalData6_ID,UartScreen_NormalData7_ID,UartScreen_NormalData8_ID};

uint32_t recIds[8];
uint8_t recLines = 0;

uint16_t TranslateIntSendString(uint16_t id, int32_t source, uint8_t *targetChar)
{
    
	uint16_t totalLength = 0,sourceLength=0;
	uint8_t sourceStr[4];
    uint8_t i = 0;

	sourceLength = IntToChar_g(source, sourceStr);

	totalLength = 9 + 2 + sourceLength;

	memcpy(targetChar, uartscreenstart,5);

	targetChar[5] = (id >> 8) & 0xff;
	targetChar[6] = id & 0xff;
	for (i = 0; i<sourceLength; i++)
	{
		targetChar[7 + i] = sourceStr[i];
	}

	for (i = 0; i<4; i++)
		targetChar[7 + sourceLength + i] = uartscreenend[i];

	return totalLength;
}

uint16_t TranslateStrSendString(uint16_t id,uint8_t *sourceStr, int32_t sourceLength, uint8_t *targetChar)
{
    uint16_t totalLength;
	uint8_t i = 0;
    
	if(sourceStr[sourceLength-1]=='\0')
		sourceLength = sourceLength - 1;
	totalLength = 11 + sourceLength;
    
	memcpy(targetChar, uartscreenstart,5);;


	(targetChar)[5] = (id >> 8) & 0xff;
	(targetChar)[6] = id & 0xff;
	for (i = 0; i<sourceLength; i++)
	{
		(targetChar)[7 + i] = sourceStr[i];
	}

	for (i = 0; i<4; i++)
		(targetChar)[7 + sourceLength + i] = uartscreenend[i];

	return totalLength;
}


void ClearUartScreen()
{
	uint8_t str[] = "未知",i;
	SendStrFromUartScreen(UartScreen_ProtocolType_ID,str,sizeof(str));

	for(i=0;i<sizeof(zeroIds)/sizeof(uint16_t);i++)
	{
		uint8_t strZero[] = "0";
		SendStrFromUartScreen(zeroIds[i],strZero,sizeof(strZero));
	}

	for(i=0;i<sizeof(dataShow)/sizeof(uint16_t);i++)
	{
		uint8_t strDataShow[] = "接收数据";
		SendStrFromUartScreen(dataShow[i],strDataShow,sizeof(strDataShow));
	}

}


void SendIntFromUartScreen(uint16_t id, int32_t data)
{
	uint16_t targetLength = TranslateIntSendString(id,data,uartscreenDatas);

	Uart_WriteBytes_g(uartscreenDatas,targetLength); //Send from LINFlex_1
}


void SendStrFromUartScreen(uint16_t id, uint8_t *data, int32_t length)
{
	uint16_t targetLength = TranslateStrSendString(id,data,length,uartscreenDatas);

	Uart_WriteBytes_g(uartscreenDatas,targetLength); //Send from LINFlex_1

}

void SendBytesFromUartScreen(uint16_t id,  uint8_t *data, int32_t length)
{
	uint8_t targetHexData[20];


	//字节转字符串
	uint16_t hexDatalLength = ByteToHexStr_g(data, length, targetHexData,0);

	//int32_t targetIntLength = TranslateUIntSendString(id, canid, &targetInt);
	uint16_t targetTotalLength = TranslateStrSendString(id, targetHexData, hexDatalLength, uartscreenDatas);


	Uart_WriteBytes_g(uartscreenDatas,targetTotalLength);
	
}

void SendCANDataFromUartScreen(uint16_t id, uCAN1_MSG *receiveCanMsg)
{
    uint8_t data[receiveCanMsg->frame.dlc];
    
	uint16_t totalLength;
	uint8_t targetHexUInt[4];
	uint8_t targetHexData[50];
	uint8_t totalHexStr[54];

    //can id to bytes
	uint16_t hexUIntLength = UIntToHexStr_g(receiveCanMsg->frame.id, targetHexUInt);

	//can data 字节转字符串
	uint16_t hexDatalLength = ByteToHexStr_g(data, receiveCanMsg->frame.dlc, targetHexData,0);

	totalLength = hexUIntLength + hexDatalLength+1;


	memcpy(totalHexStr, targetHexUInt, hexUIntLength);
	totalHexStr[hexUIntLength] = ':';
	memcpy(totalHexStr + hexUIntLength + 1, targetHexData, hexDatalLength);


	//int32_t targetIntLength = TranslateUIntSendString(id, canid, &targetInt);
	totalLength = TranslateStrSendString(id, totalHexStr, totalLength, uartscreenDatas);

	Uart_WriteBytes_g(uartscreenDatas,totalLength);

}

void SendCANDataFromUartScreenAutoShow(uCAN1_MSG *receiveCanMsg)
{
	uint8_t i;
	for(i=0;i<recLines;i++)
	{
		if(receiveCanMsg->frame.id==recIds[i])
		{
			SendCANDataFromUartScreen(UartScreen_NormalData1_ID+i,receiveCanMsg);
			return;
		}

	}
	if(recLines>7)
		return;

	recIds[recLines]=receiveCanMsg->frame.id;
	SendCANDataFromUartScreen(UartScreen_NormalData1_ID+recLines,receiveCanMsg);
	recLines++;
}
